package cn.anjubao.cp.api.service;

import cn.anjubao.cp.api.common.Constants;
import cn.anjubao.cp.api.dto.*;
import cn.anjubao.cp.api.entity.ParkingImage;
import cn.anjubao.cp.api.entity.PayRecord;
import cn.anjubao.cp.api.feign.ParkingLotFeignClient;
import cn.anjubao.cp.api.mapper.ParkingImageMapper;
import cn.anjubao.cp.api.mapper.ParkingRecordMapper;
import cn.anjubao.cp.api.mapper.PayRecordMapper;
import cn.anjubao.cp.common.utils.DateHelper;
import com.anjubao.parking.municipal.common.dto.ApiResult;
import com.anjubao.parking.municipal.parkinglot.dto.ParkingTotalFeeDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class ParkingRecordService {
    @Autowired
    private ParkingRecordMapper parkingRecordMapper;
    @Autowired
    private ParkingImageMapper parkingImageMapper;
    @Autowired
    private PayRecordMapper payRecordMapper;
    @Autowired
    private ParkingLotFeignClient parkingLotFeignClient;
    @Value("${imgPrefix}")
    private String imgPrefix;


    /**
     * 获取订单列表
     * @param reqForm
     * @return
     */
    public List<OrderListResDto> getOrderList(OrderListReqForm reqForm){
        List<OrderListResDto> orderListResDtos= parkingRecordMapper.getOrderList(reqForm);
        for (OrderListResDto resDto:orderListResDtos) {
            int countFee=0;
            int oweFee=0;
            //1.进行中
            if (resDto.getOrderState()==1){
                ApiResult<ParkingTotalFeeDto>  apiResult=parkingLotFeignClient.getParkingTotalFeeWithString(resDto.getParkingLotCode(),
                        DateHelper.getDateString(resDto.getCarInTime()),DateHelper.getDateString(new Date()));
                if (resDto.getParkingType()==2){//低杆
                    countFee=apiResult.getResult().getTotalFee();
                }
                if (resDto.getParkingType()==1){//中杆
                    int parkingFee=apiResult.getResult().getTotalFee();
                    if (resDto.getEnableMonitor()){//开启哨兵
                        countFee = ((resDto.getParkingTime() - 1)/Constants.monitor_time_section + 1) * Constants.monitor_Fee_section+parkingFee ;
                    }else {
                        countFee=parkingFee;
                    }
                }
            }
            //2.待缴费
            if (resDto.getOrderState()==2){
                PayRecord payRecord=payRecordMapper.getByParkingRecordId(resDto.getParkingRecordId(),0);
                if (payRecord!=null)oweFee=payRecord.getAmount();
            }
            //3.已完成
            if (resDto.getOrderState()==3){
                List<PayRecord> payRecords=payRecordMapper.getListByParkingRecordId(resDto.getParkingRecordId());
               if (payRecords!=null && payRecords.size()>0){
                   countFee=payRecords.stream().filter(i->i.getPayState()).
                           collect(Collectors.summingInt(PayRecord::getAmount));
               }
            }
            resDto.setCountFee(countFee);
            resDto.setOweFee(oweFee);
        }
        return orderListResDtos;
    }

    /**
     * 获取订单详情
     * @param parkingRecordId
     * @return
     */
    public OrderDetailsResDto getOrderDetails(String parkingRecordId){
        OrderDetailsResDto resDto=parkingRecordMapper.getOrderDetails(parkingRecordId);
        if (resDto==null) return null;
        int countFee=0;
        int oweFee=0;
        int parkingFee=0;
        int monitorFee=0;
        int prePay=0;
        int afterPay=0;
        PayData payData=null;
        List<CarDetailData> carInData=null;
        List<CarDetailData> carOutData=null;
        //一.进出场图片处理
        //1.1低杆
        if (resDto.getParkingType()==2){
            carInData=parkingImageMapper.getPolePic(imgPrefix,parkingRecordId,1,2);
            if (resDto.getOrderState()!=1){
                carOutData=parkingImageMapper.getPolePic(imgPrefix,parkingRecordId,2,2);
            }
        }
        //1.2中杆处理
        if (resDto.getParkingType()==1){
            carInData=parkingImageMapper.getPolePic(imgPrefix,parkingRecordId,1,1);
            ParkingImage.sortByCaptureTimeAsc(carInData);
            if (resDto.getOrderState()!=1){
                carOutData=parkingImageMapper.getPolePic(imgPrefix,parkingRecordId,2,1);
                ParkingImage.sortByCaptureTimeAsc(carOutData);
            }
        }
        //二.费用处理
        //2.1进行中
        if (resDto.getOrderState()==1){
            ApiResult<ParkingTotalFeeDto>  apiResult=parkingLotFeignClient.getParkingTotalFeeWithString(resDto.getParkingLotCode(),
                    DateHelper.getDateString(resDto.getCarInTime()),DateHelper.getDateString(new Date()));
            if (resDto.getParkingType()==2){//低杆
                countFee=apiResult.getResult().getTotalFee();
            }
            if (resDto.getParkingType()==1){//中杆
                parkingFee=apiResult.getResult().getTotalFee();
                if (resDto.getEnableMonitor()){//开启哨兵
                    monitorFee=((resDto.getParkingTime() - 1)/Constants.monitor_time_section + 1) * Constants.monitor_Fee_section;
                    countFee=parkingFee+monitorFee;
                }else {
                    countFee=parkingFee;
                }
                PayRecord prePayRecord=payRecordMapper.getPrePayRecord(parkingRecordId);
                if (prePayRecord!=null) prePay=prePayRecord.getAmount();
            }

        }
        //2.2待缴费
        if (resDto.getOrderState()==2){
            PayRecord owePayRecord=payRecordMapper.getByParkingRecordId(parkingRecordId,0);
            if (resDto.getParkingType()==2){//低杆
                if (owePayRecord!=null)oweFee=owePayRecord.getAmount();
                countFee=oweFee;
            }
            if (resDto.getParkingType()==1){//中杆
                PayRecord prePayRecord=payRecordMapper.getPrePayRecord(parkingRecordId);
                if (prePayRecord!=null) prePay=prePayRecord.getAmount();
                oweFee=owePayRecord.getAmount();
                countFee=prePay+oweFee;
            }
        }
        //2.3已完成
        if (resDto.getOrderState()==3){
            List<PayRecord> completePayRecords=payRecordMapper.getListByParkingRecordId(parkingRecordId);
            if (resDto.getParkingType()==2){//低杆
                countFee=completePayRecords.get(0).getAmount();
            }
            if (resDto.getParkingType()==1){//中杆
                if (completePayRecords!=null && completePayRecords.size()>0){
                    for (PayRecord payRecord: completePayRecords) {
                        countFee=countFee+payRecord.getAmount();
                        if (payRecord.getPayType()==1){
                            prePay=payRecord.getAmount();
                        }else if (payRecord.getPayType()==2){
                            afterPay=payRecord.getAmount();
                        }
                    }
                }
            }
            payData=new PayData();
            payData.setPaySource(completePayRecords.get(0).getPaySource());
            payData.setPayTime(completePayRecords.get(0).getPayTime());
            payData.setPayFee(countFee);
        }
        resDto.setCountFee(countFee);
        resDto.setOweFee(oweFee);
        resDto.setParkingFee(parkingFee);
        resDto.setMonitorFee(monitorFee);
        resDto.setPrePay(prePay);
        resDto.setAfterPay(afterPay);
        resDto.setPayData(payData);
        resDto.setCarInData(carInData);
        resDto.setCarOutData(carOutData);
        return resDto;
    }

    /**
     * 获取电子哨兵列表
     * @param reqForm
     * @return
     */
    public List<MonitorListResDto> getMonitorList(MonitorListReqForm reqForm){
        List<MonitorListResDto> resDtos=parkingRecordMapper.getMonitorList(reqForm);
        for (MonitorListResDto resDto: resDtos ) {
            int alertTime=parkingImageMapper.countAlertTime(resDto.getParkingRecordId());
            resDto.setAlertTimes(alertTime);
        }
        return resDtos;
    }

    /**
     * 获取电子哨兵详情
     * @param parkingRecordId
     * @return
     */
    public MonitorDetailsResDto getMonitorDetails(String parkingRecordId){
        MonitorDetailsResDto resDto=new MonitorDetailsResDto();
        MonitorParkingTimeDto timeDto=parkingRecordMapper.getMonitorParkingTime(parkingRecordId);
        List<MonitorDetailsData> detailsData=parkingImageMapper.getAlertPiC(parkingRecordId);
        resDto.setMonitorTime(timeDto!=null?timeDto.getParkingTime():0);
        resDto.setAlertTimes(detailsData.size());
        resDto.setDetailsData(detailsData);
        return resDto;
    }

    /**
     * 获取报警记录
     * @param alertRecordId
     * @return
     */
    public AlertRecordResForm getAlertRecord(String alertRecordId){
        return parkingImageMapper.getAlertRecord(imgPrefix,alertRecordId);
    }

    /**
     * 获取订单列表total
     * @param reqForm
     * @return
     */
    public int getOrderListTotal(OrderListReqForm reqForm){
        Integer total=parkingRecordMapper.getOrderListTotal(reqForm);
        if (total==null) total=0;
        return total;
    }

    /**
     * 获取哨兵列表total
     * @param reqForm
     * @return
     */
    public int getMonitorListTotal(MonitorListReqForm reqForm){
        Integer total=parkingRecordMapper.getMonitorListTotal(reqForm);
        if (total==null) total=0;
        return total;
    }
}
